#include "HorizontalScrollView.h"

#include "../_Luly.h"
HorizontalScrollView::HorizontalScrollView(
    std::string uiObjectId, HorizontalScrollView::Alignment alignment)
    : UIObject(uiObjectId), alignment(alignment) {
  layout = new Layout("scrollV_" + uiObjectId, Layout::Arrangement::HORIZONTAL);
  layout->setWidthMode(UIObject::WHMode::FIT);
  UIObject::addChild(layout);
}
void HorizontalScrollView::init() {
  UIObject::setRelativeXY(0, 0);
  layout->setRelativeXY(0, 0);
  UIObject::init();
}

void HorizontalScrollView::event() {
  SDL_Event& event = _Luly::getInstance().current_event;

#ifndef _ANDROID_PLATFORM__
  int mouseX = 0;
  int mouseY = 0;
  SDL_GetMouseState(&mouseX, &mouseY);
  mouseX = int(mouseX / _Luly::getInstance().scale);
  mouseY = int(mouseY / _Luly::getInstance().scale);
  if (event.wheel.type == SDL_MOUSEWHEEL) {
    if (isPointWithin(mouseX, mouseY)) {
      if (alignment == Alignment::Left) {
        current_deltaX = HorizontalScrollView::x - layout->getX();
        if (current_deltaX - 4 * event.wheel.preciseY <= maxDeltaX) {
          if (current_deltaX - 4 * event.wheel.preciseY < 0) {
            layout->setRelativeX(0);
          } else {
            layout->setX(layout->getX() + 4 * event.wheel.preciseY);
          }
        } else {
          layout->setX(x - maxDeltaX);
        }
      } else {
        current_deltaX = layout->getX() + layout->getWidth() - (x + width);
        if (current_deltaX + 4 * event.wheel.preciseY <= maxDeltaX) {
          if (current_deltaX + 4 * event.wheel.preciseY < 0) {
            layout->setX(x - maxDeltaX);

          } else {
            layout->setX(x - (maxDeltaX - current_deltaX) +
                         4 * event.wheel.preciseY);
          }
        } else {
          layout->setRelativeX(0);
        }
      }
    }
  }

#else
  // bool couldChildEvent = true;
  if (event.tfinger.type == SDL_FINGERMOTION) {
    //如果有垂直移动，那么就移动，并禁止子对象的事件
    if (event.tfinger.dx != 0) {
      //如果移动的手指在fingeIds里面
      if (fingerIds.find(event.tfinger.fingerId) != fingerIds.end()) {
        // couldChildEvent = false;  //禁止子对象事件
        //移动视图
        if (alignment == Alignment::Left) {
          //如果是左对齐
          current_deltaX = HorizontalScrollView::x - layout->getX();
          if (current_deltaX -
                  event.tfinger.dx * _Luly::getInstance().windowWidth <=
              maxDeltaX) {
            if (current_deltaX -
                    event.tfinger.dx * _Luly::getInstance().windowWidth <
                0) {
              layout->setRelativeX(0);
            } else {
              layout->setX(layout->getX() +
                           event.tfinger.dx * _Luly::getInstance().windowWidth);
            }
          } else {
            layout->setX(x - maxDeltaX);
          }
        } else {
          //如果是右对齐
          current_deltaX = layout->getX() + layout->getWidth() - (x + width);
          if (current_deltaX +
                  event.tfinger.dx * _Luly::getInstance().windowWidth <=
              maxDeltaX) {
            if (current_deltaX +
                    event.tfinger.dx * _Luly::getInstance().windowWidth <
                0) {
              layout->setX(x - maxDeltaX);
            } else {
              layout->setX(x - (maxDeltaX - current_deltaX) +
                           event.tfinger.dx * _Luly::getInstance().windowWidth);
            }
          } else {
            layout->setRelativeX(0);
          }
        }
      }
    }
  }
  switch (event.tfinger.type) {
    case SDL_FINGERDOWN: {
      //按下时，如果手指在范围内，则记录该手指
      if (isPointWithin(event.tfinger.x * _Luly::getInstance().windowWidth,
                        event.tfinger.y * _Luly::getInstance().windowHeight)) {
        fingerIds.insert(event.tfinger.fingerId);
      }
      break;
    }
    case SDL_FINGERUP: {
      //抬起时，移除手指(set无需判断是否在里面，如果不在的话，只是返回end())
      fingerIds.erase(event.tfinger.fingerId);
      // if (fingerIds.size() == 0) {
      //   couldChildEvent = true;
      // }else{
      //   couldChildEvent = false;
      // }

      break;
    }
  }
    // if (couldChildEvent) {
    //   //如果为true
    //   UIObject::event();  //执行子对象事件操作
    // }
#endif

  UIObject::event();
}

void HorizontalScrollView::drawArea(const Rect& area) {
  //同步layout的位置

  if (alignment == Alignment::Left) {
    layout->setX(x - current_deltaX);
  } else {
    layout->setX(x - (maxDeltaX - current_deltaX));
  }
  layout->setY(y);

  //如果不在允许的范围内，则强制移回范围内
  if (HorizontalScrollView::x - layout->getX() <= maxDeltaX) {
    if (HorizontalScrollView::x - layout->getX() < 0) {
      if (alignment == Alignment::Left) {
        layout->setRelativeX(0);
      } else {
        layout->setX(x - maxDeltaX);
      }
    }
  } else {
    if (alignment == Alignment::Right) {
      layout->setX(x - maxDeltaX);
    } else {
      layout->setRelativeX(0);
    }
  }

  //设置maxDeltaX
  int tempDeltaX = layout->getWidth() - width;
  maxDeltaX = (tempDeltaX < 0) ? 0 : tempDeltaX;

  height = layout->getHeight();
  if (layout->getWidth() > parent_object->getContentWidth()) {
    HorizontalScrollView::widthMode = WHMode::FILL;
  } else {
    HorizontalScrollView::width = layout->getWidth();
  }

  //子对象只绘制在HorizontalScrollView里的部分
  // setRelativeXY(50,52);
  UIObject::drawArea(
      area);  //只传入area就行了，UIObject会自动处理传入子对象的area
}